package com.database;

import android.app.Activity;
import android.os.Bundle;

import static android.provider.BaseColumns._ID;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.TextView;

public class MainDatabase extends Activity {
	  DatabaseHelper eventsData;
	  PlantInfoSmallDatabase plantSmall;
	  TextView output;
	  TextView output2;
	  private final String MY_DATABASE_NAME = "wateringPlanDatabase";

	  @Override
	  public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.main);

	    output = (TextView) findViewById(R.id.output);
	    output2 = (TextView) findViewById(R.id.output2);
	    

	    eventsData = new DatabaseHelper(this);
	    plantSmall = new PlantInfoSmallDatabase(this);
	    addEvent("Hello Android Event");
	    addPlant();
	   // Cursor plantCursor = getPlantEvents();
	    Cursor cursor = getEvents();
	    showEvents(cursor);
	  //  showEventsPlant(plantCursor);
	  }
	  
	  @Override
	  public void onDestroy() {
	    eventsData.close();
	    plantSmall.close();
	  }

	  private void addEvent(String title) {
	    SQLiteDatabase db = plantSmall.getWritableDatabase();
	    ContentValues values = new ContentValues();
	    values.put(DatabaseHelper.TIME, System.currentTimeMillis());
	    values.put(DatabaseHelper.TITLE, title);
	    db.insert(DatabaseHelper.TABLE, null, values);

	  }
	  
	  private void addPlant() {
		    SQLiteDatabase db = eventsData.getWritableDatabase();
		    ContentValues values = new ContentValues();
		    values.put(PlantInfoSmallDatabase.PLANT_TYPE_ID, 2);
		    values.put(PlantInfoSmallDatabase.SEASON,"Winter");
		    values.put(PlantInfoSmallDatabase.TEMPERATURE, "Cold");				
		    values.put(PlantInfoSmallDatabase.HUMIDITY, 1);
		    values.put(PlantInfoSmallDatabase.LIGHT_NEEDS,1);
		    values.put(PlantInfoSmallDatabase.SOIL,"Dry");
		    values.put(PlantInfoSmallDatabase.WATERING, 1 );
		    values.put(PlantInfoSmallDatabase.FERTILIZER, "none");
		    db.insert(PlantInfoSmallDatabase.TABLE, null, values);

		  }

	  private Cursor getEvents() {
	    SQLiteDatabase db = eventsData.getReadableDatabase();
	    Cursor cursor = db.query(DatabaseHelper.TABLE, null, null, null, null,
	        null, null);
	    
	    startManagingCursor(cursor);
	    return cursor;
	  }
	  
	  private Cursor getPlantEvents(){
		  SQLiteDatabase db = plantSmall.getReadableDatabase();
		    Cursor cursor = db.query(PlantInfoSmallDatabase.TABLE, null, null, null, null,
		        null, null);
		    
		  startManagingCursor(cursor);
		  return cursor;
	  }

	  private void showEvents(Cursor cursor) {
	    StringBuilder ret = new StringBuilder("Saved Events:\n\n");
	    while (cursor.moveToNext()) {
	      long id = cursor.getLong(0);
	      long time = cursor.getLong(1);
	      String title = cursor.getString(2);
	      ret.append(id + ": " + time + ": " + title + "\n");
	    }
	    output.setText(ret);
	  }
	  
	  private void showEventsPlant(Cursor cursor) {
		    StringBuilder ret = new StringBuilder("Saved Events:\n\n");
		    while (cursor.moveToNext()) {
		    	int id = cursor.getInt(0);
			    String sezon = cursor.getString(1);
			    String temp = cursor.getString(1);
		      ret.append(id + ": " + sezon  + ": " + temp + "\n" );
		    }
		    output2.setText(ret);
		  }
	}